package com.romadan.core.nonlinearanalys.fet.volterra.logic;

import java.util.ArrayList;
import java.util.List;

public class GraphType {
	private int graph_id;
	private List<EdgeType> edges;
	private List<NodeType> nodes;
	
	public GraphType(int graph_id) {
		this.graph_id = graph_id;
	}
	
	public GraphType(int graph_id, List<EdgeType> edges) {
		this.graph_id = graph_id;
		this.edges = edges;
	}
	
	public GraphType(int graph_id, List<EdgeType> edges, List<NodeType> nodes) {
		this.graph_id = graph_id;
		this.edges = edges;
		this.nodes = nodes;
	}
	
	public int getCountEdges() {
		return getEdges().size();
	}
	
	public int getCountNodes() {
		return getNodes().size();
	}

	public List<EdgeType> getEdges() {
		return edges;
	}

	public void setEdges(List<EdgeType> edges) {
		this.edges = edges;
	}

	public List<NodeType> getNodes() {
		if (nodes != null) {
			return nodes;
		} else if (edges != null){
			nodes = new ArrayList<NodeType>();
			for (EdgeType edge : edges) {
				if (!nodes.contains(edge.getA_node())) {
					nodes.add(edge.getA_node());
				}
				if (!nodes.contains(edge.getZ_node())) {
					nodes.add(edge.getZ_node());
				}
			}
			return nodes;
		} else {
			return null;
		}
	}

	public void setNodes(List<NodeType> nodes) {
		this.nodes = nodes;
	}

	public int getGraph_id() {
		return graph_id;
	}
	
	public EdgeType getEdgeById(int id) {
		for (EdgeType edge : edges) {
			if (edge.getId() == id) {
				return edge;
			}
		}
		
		return null;
	}
	
	public NodeType getNodeById(int id) {
		for (NodeType node : nodes) {
			if (node.getId() == id) {
				return node;
			}
		}
		
		return null;
	}

	@Override
	public String toString() {
		return "GraphType [graph_id=" + graph_id + ", edges=" + edges
				+ ", nodes=" + nodes + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + graph_id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GraphType other = (GraphType) obj;
		if (graph_id != other.graph_id)
			return false;
		return true;
	}
	
	
} 
